﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace hwapp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            while (true)
            {
                switch (ShowMenu())
                {
                    case ("1"):
                        CreateControllerAndView();
                        break;
                    case ("2"):
                        InitialFile();
                        break;
                    case ("3"):
                        DataBaseMigration();
                        break;
                    case ("0"):
                        return;
                }

            }
        }
        public static string ShowMenu()
        {
            List<string> services = new List<string> { "CreateControllerAndView", "Initial .csproj", "DataBase Migration" };
            services.ForEach(str => Console.WriteLine($"{services.IndexOf(str) + 1}.{str}"));
            Console.WriteLine("0.EXIT");
            Console.Write("input:");
            string input = Console.ReadLine();
            return input;
        }
        public static string SelectDir()
        {
            string[] files = Directory.GetDirectories("../");
            for (int i = 0; i < files.Length; i++)
            {
                Console.WriteLine($"{i + 1} : {files[i]}");
            }
            string numS = Console.ReadLine();
            int num = int.Parse(numS) - 1;
            string dir = files[num];
            return dir;
        }
        public static void DataBaseMigration()
        {
            string dir = SelectDir();
            Console.Write("Add something:");
            string message = Console.ReadLine();
            Console.WriteLine($"dotnet ef migrations add {message}".Bash(dir));
            Console.Write("update?(y/N):");
            string b = Console.ReadLine();
            if (b == "y")
            {
                Console.WriteLine("dotnet ef database update".Bash(dir));
            }

        }
        public static void CreateControllerAndView()
        {
            string dir = SelectDir();
            List<string> fileModel = Directory.GetFiles($"{dir}/Models").ToList();
            fileModel.Remove(fileModel.Find(str => str.Contains("ErrorViewModel")));
            string context = fileModel.Find(str => str.Contains("Context.cs"));
            fileModel.Remove(context);
            Regex regex = new Regex(@"/(\w+)[.]cs$");
            context = regex.Match(context).Groups[1].Value;
            for (int i = 0; i < fileModel.Count; i++)
            {
                fileModel[i] = regex.Match(fileModel[i]).Groups[1].Value;
            }
            fileModel.ForEach(str => Console.WriteLine(CreateCmd(str, context).Bash(dir)));
        }
        public static void InitialFile()
        {
            string dir = SelectDir();
            List<string> fileList = Directory.GetFiles($"{dir}").ToList();
            string file = fileList.Find(str => str.Contains(".csproj"));
            string filePath = $"{dir}/{file}";
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            File.Copy("csprojTemple", filePath);
            Console.WriteLine("dotnet restore".Bash(dir));
        }
        public static string CreateCmd(string files, string context)
        {
            string cmd = $"dotnet aspnet-codegenerator controller -name {files}sController -m {files} -dc {context} --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries";
            return cmd;
        }
    }
    public static class ShellHelper
    {
        public static string Bash(this string cmd, string dir)
        {
            var escapedArgs = cmd.Replace("\"", "\\\"");

            var process = new Process()
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = "/bin/bash",
                    Arguments = $"-c \"{escapedArgs}\"",
                    RedirectStandardOutput = true,
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    WorkingDirectory = $"{dir}"
                }
            };
            process.Start();
            string result = process.StandardOutput.ReadToEnd();
            process.WaitForExit();
            return result;
        }
    }
}
